package android.bluebox.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluebox.R;
import android.bluebox.model.Attribute;
import android.bluebox.model.DatabaseBox;
import android.bluebox.model.NetworkBox;
import android.bluebox.model.StaticBox;
import android.bluebox.model.Tag;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class FirstView extends Activity {

	ImageButton btnNew;
	ImageButton btnLoginWindows;
	ImageButton btnWorkspace;
	ImageButton btnTag;
	ImageButton btnAttribute;
	ImageButton btnBrowser;

	TextView txtHeader;
	TextView txtWorkspace;
	TextView txtTag;
	TextView txtAttribute;

	Intent intent;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.first);

		txtHeader = (TextView) findViewById(R.id.txt_bluebox_header);
		txtWorkspace = (TextView) findViewById(R.id.f_txt_workspace);
		txtTag = (TextView) findViewById(R.id.f_txt_tag);
		txtAttribute = (TextView) findViewById(R.id.f_txt_attribute);

		btnNew = (ImageButton) findViewById(R.id.first_btn_new);
		btnNew.setOnClickListener(createNew);

		btnBrowser = (ImageButton) findViewById(R.id.first_btn_browser);
		btnBrowser.setOnClickListener(autofill);
		
		btnWorkspace = (ImageButton) findViewById(R.id.first_btn_workspace);
		btnWorkspace.setOnClickListener(workspace);

		btnTag = (ImageButton) findViewById(R.id.first_btn_tag);
		btnTag.setOnClickListener(tag);

		btnAttribute = (ImageButton) findViewById(R.id.first_btn_attribute);
		btnAttribute.setOnClickListener(attribute);

		btnLoginWindows = (ImageButton) findViewById(R.id.first_btn_loginwindows);
		btnLoginWindows.setOnClickListener(loginWindows);

		StaticBox.netBox = new NetworkBox();
	}

	public void loadInfo() {
		StaticBox.dbBox = new DatabaseBox(this);

		txtHeader.setText(StaticBox.getCurrentWorkspaceName());

		txtWorkspace.setText("Workspaces ("
				+ StaticBox.dbBox.getNumberOfWorkspaces()
				+ ")");	

		txtTag.setText("Tags ("
				+ StaticBox.dbBox.getNumberOfTags()
				+ ")");

		txtAttribute.setText("Attributes ("
				+ StaticBox.dbBox.getNumberOfAttributes()
				+ ")");
	}

	public void createNewTag() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("New Tag");
		alert.setMessage("Name");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		alert.setView(edtName);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String newName = edtName.getText().toString().trim(); 
				if (newName.length() > 0) {
					createNewTag(newName);
				} else {
					Toast.makeText(getBaseContext(), "Please input tag name", Toast.LENGTH_SHORT).show();
				}
			}

			public void createNewTag(String newName) {
				if (StaticBox.dbBox.getTag(newName) == null) {
					Tag tag = new Tag();
					tag.setName(newName);
					StaticBox.dbBox.insertTag(tag);
					
					intent = new Intent(FirstView.this, TagListView.class);
					startActivity(intent);
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				// TODO Auto-generated method stub
			}
		});
		alert.show();
	}
	
	public void loginWindowsIp() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Login Windows");
		alert.setMessage("IP Address");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		alert.setView(edtName);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String ip = edtName.getText().toString().trim(); 
				if (ip.length() > 0) {
					StaticBox.dbBox.insertIpIntoList(ip);
					loginWindowsPaircode(ip);
				} else {
					Toast.makeText(getBaseContext(), "Please input IP Address", Toast.LENGTH_SHORT).show();
				}
			}
		});

		alert.setNegativeButton("History", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				final CharSequence[] listIp = StaticBox.dbBox.loadListIp();
				AlertDialog.Builder builder = new AlertDialog.Builder(FirstView.this);
				builder.setTitle("Hitory");
				builder.setItems(listIp, new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int item) {
						loginWindowsPaircode(listIp[item].toString());
					}
				});
				builder.create().show();
			}
		});
		alert.show();
	}
	
	public void loginWindowsPaircode(final String ip) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Login Windows");
		alert.setMessage("Paircode");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		alert.setView(edtName);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String paircode = edtName.getText().toString().trim(); 
				if (paircode.length() > 0) {
					boolean login = StaticBox.netBox.loginWindows(ip, paircode);
					StaticBox.makeToast(getBaseContext(), "Login " + ((login)? "successfully":"unsuccessfully"));
					StaticBox.makeToast(getBaseContext(), ip + " | " + paircode);
				} else {
					Toast.makeText(getBaseContext(), "Please input paircode", Toast.LENGTH_SHORT).show();
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				// TODO Auto-generated method stub
			}
		});
		alert.show();
	}

	public void createNewAttribute() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("New Attribute");
		alert.setMessage("Name");

		// Set an EditText view to get user Input
		final EditText edtName = new EditText(this);
		alert.setView(edtName);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				String newName = edtName.getText().toString().trim(); 
				if (newName.length() > 0) {
					createNewAttribute(newName);
				} else {
					Toast.makeText(getBaseContext(), "Please input attribute name", Toast.LENGTH_SHORT).show();
				}
			}
			public void createNewAttribute(String newName) {
				if (StaticBox.dbBox.getAttribute(newName) == null) {
					Attribute attribute = new Attribute();
					attribute.setName(newName);
					StaticBox.dbBox.insertAttribute(attribute);
					
					intent = new Intent(FirstView.this, AttributeListView.class);
					startActivity(intent);
				}
			}
		});

		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int button) {
				// TODO Auto-generated method stub
			}
		});
		alert.show();
	}

	public OnClickListener createNew = new OnClickListener() {

		@Override
		public void onClick(View v) {
			final CharSequence[] newOptions = {"New workspace", "New Tag", "New attribute"};
			AlertDialog.Builder builder = new AlertDialog.Builder(FirstView.this);
			builder.setTitle("Create new");
			builder.setItems(newOptions, new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int item) {
					switch(item) {
					/*
					 * Create new workspace
					 */
					case 0:
						intent = new Intent(FirstView.this, WorkspaceDetailView.class);
						intent.putExtra("ws_id", 0);
						startActivityForResult(intent, 0);
						break;

					case 1:
						createNewTag();
						break;

					case 2:
						createNewAttribute();
						break;
					}
					loadInfo();
				}
			});
			builder.create().show();
		}
	};

	public OnClickListener workspace = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			intent = new Intent(FirstView.this, WorkspaceListView.class);
			startActivity(intent);
		}
	};

	public OnClickListener tag = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			intent = new Intent(FirstView.this, TagListView.class);
			startActivity(intent);
		}
	};

	public OnClickListener attribute = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			intent = new Intent(FirstView.this, AttributeListView.class);
			startActivity(intent);
		}
	};

	public OnClickListener loginWindows = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			loginWindowsIp();
		}
	};
	
	public OnClickListener autofill = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			StaticBox.netBox.AutoFillBrowser();
			
		}
	};

	/*
	 * Create Option Menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.first_optionmenu, menu);
		return true;
	}

	/*
	 * Create event for Option Menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.first_change_password:
			Intent intent = new Intent(FirstView.this, ChangePasswordView.class);
			startActivity(intent);
			break;
			
		case R.id.first_backup:
			boolean backup = StaticBox.dbBox.backup();
			StaticBox.makeToast(getBaseContext(), "Backup " + ((backup)? "successfully" : "unsuccessfully") );
			break;
			
		case R.id.first_restore:
			boolean restore = StaticBox.dbBox.restore();
			StaticBox.makeToast(getBaseContext(), "Restore " + ((restore)? "successfully" : "unsuccessfully"));
			break;
		}
		return true;
	}
	
	protected void onResume() {
		super.onResume();
		loadInfo();
	}
}
